#include <stdio.h>
int main()
{
    int n,i,digit,t,k;
    scanf("%d", &k);
    while ( k-- )
    {
        scanf("%d", &n);
        digit = n % 10;
        t = n & 3;
        n %= 10;
        if ( t == 0 ) t = 4;
        for ( i = 1; i < t; i++ )
        {
            digit *= n;
        }
        printf("%d\n", digit % 10);
    }
    return 0;
}
